<?php

namespace backend\models;

use Yii;

/**
 * This is the model class for table "customer".
 *
 * @property string $id
 * @property string $url
 * @property string $company
 * @property string $contact
 * @property integer $type
 * @property integer $advantage
 * @property integer $province
 * @property resource $image
 * @property string $history
 * @property string $history_date
 * @property string $history_contact
 * @property string $history_result
 * @property string $next_date
 * @property string $project_file
 * @property string $project_province
 * @property integer $project_type
 * @property string $investment
 * @property string $volume
 * @property string $remark
 * @property string $created_at
 * @property  integer owner
 * @property integer $company_type
 * @property integer $company_level
 * @property integer $type_desc
 * @property string $principal
 * @property integer $package_type
 * @property integer $package_transfer
 * @property integer $company_province
 * @property integer $company_city
 * @property integer $review_level
 * @property string $mark_A_at
 * @property string $first_mark_A
 * @property string $tag
 *
 */
class Customer extends \yii\db\ActiveRecord
{
    public static function getDb()
    {
        return Yii::$app->get('forge');
    }
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'customer';
    }

    public function attributes()
    {
        return array_merge(parent::attributes(), ['history_date', 'history_contact', 'history_result']);
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['company',], 'required'],
//            ['company', 'unique', 'targetClass' => '\backend\models\Customer', 'message' => '机构名称已经存在',],//验证唯一性
            [['project_type', 'company_type', 'company_level', 'package_type', 'package_transfer', 'review_level', 'is_need_clear'], 'integer'],
            [['url', 'image', 'history', 'investment', 'volume', 'remark', 'type', 'advantage', 'post', 'mobile', 'principal', 'need', 'market_area'], 'string'],
            [['url', 'company', 'contact', 'history_date',], 'string', 'max' => 255],
            [['project_type', 'owner', 'company_type', 'package_type', 'package_transfer'], 'default', 'value' => 0],
//            [['project_file'], 'default', 'value' => ''],
            [['project_province', 'company_province', 'company_city',], 'default', 'value' => 0],
            [['tag', 'company_province', 'company_city', 'type', 'advantage', 'image', 'history', 'next_date', 'project_file', 'project_type', 'investment', 'volume', 'history_contact', 'remark', 'history_result', 'created_at', 'first_mark_A', 'mark_A_at', 'province', 'project_province', 'type_desc'], 'safe'],
        ];
    }


    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'url' => '机构链接',
            'company' => '机构名称',
            'company_province' => '省份',
            'company_city' => '城市',
            'company_type' => '机构分类',
            'company_level' => '机构等级',
            'contact' => '联系人',
            'post' => '职务',
            'mobile' => '电话',
            'type' => '客户类型',
            'type_desc' => '客户细分',
            'advantage' => '擅长领域',
            'province' => '业务范围',
            'image' => '名片',
            'history' => '沟通记录',
            'history_date' => '沟通日期',
            'history_contact' => '沟通联系人',
            'history_result' => '沟通结果',
            'next_date' => '下次沟通日期',
            'project_file' => '清单',
            'project_province' => '覆盖地区',
            'project_type' => '项目类型',
            'investment' => '投资额度',
            'principal' => '本金',
            'package_type' => '资产包分类',
            'package_transfer' => '资产包转让方式',
            'volume' => '项目体量',
            'remark' => '备注',
            'owner' => '创建人',
            'review_level' => '公司评级',
            'first_mark_A' => '当月任务',//首次评A月份
            'mark_A_at' => '首次评A时间',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',

            'is_need_clear' => '需求是否明确',
            'tag' => '投资标签',
            'need' => '需求描述',
            'market_area' => '投资地区',
        ];
    }

    /**
     * @inheritdoc
     * @return CustomerQuery the active query used by this AR class.
     */
    public static function find()
    {
        return new CustomerQuery(get_called_class());
    }

    public function getBackendUser()
    {
        return $this->hasOne(User::className(), ['id' => 'owner']);
    }

    public function access()
    {
        return in_array(Yii::$app->user->identity->username, Yii::$app->params['customer_access']);
    }

    public static function findByCompany($company)
    {
        return static::findOne(['company' => $company]);
    }
}
